<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Conf\ConfModel;

use Srv\Libs\Frame\Conf;

final class PayPayPalConfGroup
{
    private array $PayPayPalConfGroupNameList       = [];       // PayPayPalConf List groupName

    /**
     * @return array
     * getGroupNameList
     */
    public function getGroupNameList():array
    {
        $PayPayPalConfList          = [];
        $groupNameList              = Conf::getGroupNameList(PayPayPalConf::PREFIX);
        if(count($groupNameList) < 1) return $PayPayPalConfList;
        foreach ($groupNameList as $groupName){
            $PayPayPalConf          = $this->getByGroupName($groupName);
            if($PayPayPalConf instanceof PayPayPalConf) $PayPayPalConfList[$groupName] = $PayPayPalConf;
        }
        return $PayPayPalConfList;
    }

    /**
     * @param string $groupName
     * @return PayPayPalConf|null
     * getByGroupName
     */
    public function getByGroupName(string $groupName):?PayPayPalConf
    {
        if(strlen($groupName) < 1) return null;
        if(isset($this->PayPayPalConfGroupNameList[$groupName]) && $this->PayPayPalConfGroupNameList[$groupName] instanceof PayPayPalConf) return $this->PayPayPalConfGroupNameList[$groupName];
        $confData                                       = Conf::getGroupList(PayPayPalConf::PREFIX.$groupName.'.', false, true);
        if(count($confData) < 1) return null;
        $this->PayPayPalConfGroupNameList[$groupName]   = new PayPayPalConf($confData, $groupName);
        return $this->PayPayPalConfGroupNameList[$groupName];
    }
}